<?php

namespace lico\MVC;

use lico\Runtime\Config;
use lico\Tool\Model\Pdo;

use lico\Container;

class Model
{

    protected static $pdo;
    protected $config;

    protected $fields = "*";
    protected $querys = [];
    protected $page = "";
    protected $size = "";
    protected $orderBy = "";
    protected $sql = "";
    protected $result;

    protected $model = "";
    protected $table = "";

    protected function __construct()
    {   
        $this->config = Container::getBowl('lico\Runtime\Config')->get('database');
        if (is_null(self::$pdo)) {
            self::$pdo = new Pdo();
        }
        $this->from();
    }

    public function commit($query)
    {
    }

    /**
     * 获取调用的模型：数据表
     */
    public function from($table = "")
    {

        $modelNamespace = get_class($this);
        $this->model = substr($modelNamespace, strrpos($modelNamespace, "\\") + 1);

        $table = $table ?: strtolower($this->model);
        $this->table = $this->config['prefix'] . strtolower($table);

        return $this;
    }

    /**
     * 字段
     */
    public function field($fields)
    {
        $this->fields = $fields;
        return $this;
    }

    /**
     * 条件
     */
    public function where($query)
    {
        foreach ($query as $key => $value) {
            if (isset($this->query[$key])) {
                $this->querys[$key][] = $value;
            } else {
                $this->querys[$key] = $value;
            }
        }
        return $this;
    }

    /**
     * sql组装
     */
    public function sqlSplice()
    {

        $field = " SELECT " . $this->fields;
        $table = " FROM " . $this->table;

        $where = " ";
        foreach ($this->querys as $key => $value) {
            $where .= "`" . $key . "`='" . $value . "' AND ";
        }
        trim($where) && $where = " WHERE " . substr($where, 0, -4);

        $orderBy = " ";
        if ($this->orderBy) {
            $orderBy .= "ORDER BY " . $this->orderBy;
        }

        $limit = " ";
        if ($this->page || $this->size) {
            $limit = " LIMIT ";
            $limit .= $this->page . "," . $this->size;
        }

        $this->sql = $field . $table . $where . $orderBy . $limit;
        return $this->sql;
    }

    /**
     * 分页
     */
    public function limit($size, $page = 0)
    {
        if ($page && $page > 0) {
            $tmp = $page;
            $page = $size;
            $size = $tmp;
        } else {
            $page = 1;
        }
        $this->page = ($page - 1) * $size ?: 0;
        $this->size = $size;
        return $this;
    }

    /**
     * 排序
     */
    public function order($field, $type = "asc")
    {
        $this->orderBy = $field . " " . $type;
        return $this;
    }

    /**
     * 查询
     */
    public function select()
    {

        $this->sqlSplice();
        // p($this->sql);
        $this->result = self::$pdo->query($this->sql)->fetchAll();
        return $this->result;
    }

    public function column()
    {
        $keyIndex = stripos($this->fields, ',');
        $key = substr($this->fields, 0, $keyIndex);
        $value = substr($this->fields, $keyIndex + 1);

        $this->sqlSplice();
        $this->result = self::$pdo->query($this->sql)->column($key, $value);
        return $this->result;
    }
}
